package com.ls.fw.data.exp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.ls.fw.data.visitor.ExpressionVisitor;

public abstract class Compound extends Expression{

	
	/**
     * The set of child expressions.
     */
    protected List<Expression> children = new ArrayList<Expression>(1);

    
    /**
     * Class constructor.
     *
     * @param expressions the expressions to be evaluated
     */
    public Compound(Expression... expressions) {
        assert expressions != null : "expressions cannot be null or empty";
        this.children = new ArrayList<Expression>(Arrays.asList(expressions));
    }

    /**
     * Class constructor.
     *
     * @param expressions the expressions to be evaluated
     */
    public Compound(Collection<Expression> expressions) {
        this.children = new ArrayList<Expression>(expressions);
    }

    /**
     * Add an expression to the child set.
     *
     * @param expression the expression to add
     * @return this
     */
    public Compound add(Expression expression) {
        this.children.add(expression);
        return this;
    }

    /**
     * Returns the set of child expressions.
     *
     * @return the expression set
     */
    public List<Expression> getChildren() {
        return children;
    }
    
    
    /**
     * 是否存在条件
     * @author ls
     * 2014年12月16日下午3:32:19 
     * @return
     */
    public boolean hasCondition(){
    	return (children==null || children.isEmpty())?false:true;
    }

    @Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Compound other = (Compound) obj;
		if (children == null) {
			if (other.children != null)
				return false;
		} else if (!children.equals(other.children))
			return false;
		return true;
	}

    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((children == null) ? 0 : children.hashCode());
		return result;
	}

    public void accept(ExpressionVisitor visitor){
    	for (Expression expression : children) {
    		visitor.visit(expression);
		}
    }
}
